package tools

import (
	"encoding/json"
	"strings"
)

func ToJson(data any) string {
	json, err := json.Marshal(data)
	if err != nil {
		return ""
	}
	return string(json)
}

// ParseJSON 解析JSON字符串到对象
func ParseJSON(data string, v any) error {
	return json.Unmarshal([]byte(data), v)
}

// TrimJSONMarkers 去除JSON字符串的标记
func TrimJSONMarkers(s string) (string, bool) {
	trimmed := strings.TrimSpace(s)
	if strings.HasPrefix(trimmed, "```json") {
		trimmed = strings.TrimPrefix(trimmed, "```json")
	}
	if strings.HasSuffix(trimmed, "```") {
		trimmed = strings.TrimSuffix(trimmed, "```")
	}
	return strings.TrimSpace(trimmed), true
}
